/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plash.events;

import java.util.ArrayList;
import plash.utils.Debug;
import plash.utils.Logger;

/**
 *
 * @author User
 */
public class EventDispatcher
{

    private ArrayList<ArrayList<IEventListener>> _listeners;
    private ArrayList<String> _eventNames;

    public EventDispatcher()
    {
        
    }

    /**
     * @return void
     * @param none
     * @description The below method is for the convenience of creating the
     * lists that maintains the listeners and the eventNames. This is to ensure
     * that the lists are created only if they are to be used.
     */
    private void _createLists()
    {
        if (_listeners == null)
        {
            _listeners = new ArrayList<ArrayList<IEventListener>>();
            _eventNames = new ArrayList<String>();
        }
    }

    public void addEventListener(String eventName, IEventListener listener)
    {
        _createLists();
        /**
         * Check if the eventName is already subscribed. If it does not create a list
         * which will add the listeners interested in the particular eventName. Else
         * find the index where the list is maintained for tracking the listeners subscribed
         * to the particular event name and push the latest one into the list to be notified
         * during the occurrence of the event
         */
        if (!_eventNames.contains(eventName))
        {
            ArrayList<IEventListener> thisParticularEventSuscribers = new ArrayList<IEventListener>();            
            thisParticularEventSuscribers.add(listener);
            _eventNames.add(eventName);
           _listeners.add(thisParticularEventSuscribers);
        } 
        /**
         * if does not exist 
         */
        else
        {
            int index = _eventNames.indexOf(eventName);
            ArrayList<IEventListener> interestedListeners = _listeners.get(index);
            
            if(!interestedListeners.contains(listener))
            {
                interestedListeners.add(listener);
            }
            else
            {
                if(Debug.debug_enabled)
                {
                    Logger.log("The listener has already subscribed to the event "+eventName);
                }
            }
        }
    }
    
    public void removeEventListener(String eventName, IEventListener listener) 
    {
        _createLists();
        
        if(_eventNames.contains(eventName))
        {
            int index = _eventNames.indexOf(eventName);
            ArrayList<IEventListener> interestedListeners = _listeners.get(index);
            if(interestedListeners.contains(listener))
            {
                interestedListeners.remove(listener);
            }
            else
            {
                if(Debug.debug_enabled)
                {
                    Logger.log("This listener had never subscribed to the event "+eventName);
                }
            }
        }
        else
        {
            if(Debug.debug_enabled)
            {
                Logger.log("There was never an event "+eventName+" registered with this object");
            }
        }
    }
    
    public void dispatchEvent(Event evt)
    {
        _createLists();
        if(_eventNames.size() > 0)
        {
            if(_eventNames.contains(evt.name))
            {
                int index = _eventNames.indexOf(evt.name);
                ArrayList<IEventListener> listeners = _listeners.get(index);
                evt.target = this;
                for(IEventListener listener : listeners)
                {
                    listener.event(evt);
                }
            }
            else
            {
                if(Debug.debug_enabled)
                {
//                    Logger.log("There is no use dispatching this event as there are not listeners subscribed");
                }
            }
        }
    }
}
